package com.jmyd.coupon.server.controller;

import com.jmyd.commons.base.vo.PageBean;
import com.jmyd.commons.base.vo.Result;
import com.jmyd.commons.constants.BaseUrlConstants;
import com.jmyd.coupon.server.domain.dto.*;
import com.jmyd.coupon.server.domain.vo.SceneCouponDrawRecordVo;
import com.jmyd.coupon.server.domain.vo.SceneCouponUseRecordVo;
import com.jmyd.coupon.server.domain.vo.SceneCouponVo;
import com.jmyd.coupon.server.service.ISceneCouponService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 优惠券管理相关接口
 *
 * @author samphin
 * @date 2019-8-28 16:24:10
 */
@RestController
@RequestMapping(BaseUrlConstants.BASE_ADMIN_PREFIX + "/coupons")
public class SceneCouponController {

    @Resource
    private ISceneCouponService sceneCouponService;

    /**
     * 创建优惠券
     *
     * @author samphin
     * @since 2021-7-15 09:17:14
     */
    @PostMapping
    public Result<Boolean> save(@RequestBody @Validated SceneCouponDto creditCouponModuleDto, HttpServletRequest request) {
        boolean bl = this.sceneCouponService.save(creditCouponModuleDto, request);
        return bl ? Result.ok() : Result.failure();
    }

    /**
     * 修改优惠券
     *
     * @author samphin
     * @since 2021-7-15 09:17:14
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody @Validated SceneCouponUpdateDto creditCouponModuleDto, HttpServletRequest request) {
        boolean bl = sceneCouponService.updateById(creditCouponModuleDto, request);
        return bl ? Result.ok() : Result.failure();
    }

    /**
     * 查询优惠券列表
     *
     * @param queryDto 查询参数
     * @author samphin
     * @since 2021-7-15 09:17:14
     */
    @GetMapping
    public Result<PageBean<SceneCouponVo>> queryPageList(@ModelAttribute SceneCouponQueryDto queryDto, HttpServletRequest request) {
        PageBean<SceneCouponVo> pageBean = sceneCouponService.queryPageList(queryDto, request);
        return new Result<>(pageBean);
    }

    /**
     * 删除优惠券
     *
     * @param id 模板ID
     * @author samphin
     * @since 2021-7-15 09:17:14
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean bl = this.sceneCouponService.deleteById(id);
        return bl ? Result.ok() : Result.failure();
    }

    /**
     * 查看优惠券详情
     *
     * @param id 优惠券ID
     * @author samphin
     * @date 2019-9-11 09:43:04
     */
    @GetMapping("/{id}")
    public Result<SceneCouponVo> queryDetail(@PathVariable Long id) {
        SceneCouponVo creditCouponModuleVo = sceneCouponService.queryById(id);
        return new Result<>(creditCouponModuleVo);
    }

    /**
     * 领取免单优惠券接口
     *
     * @author samphin
     * @since 2021-7-15 09:17:14
     */
    @PostMapping("/draw/free")
    public Result<Boolean> drawFreeOfChargeCoupon(HttpServletRequest request) {
        boolean bl = this.sceneCouponService.drawFreeOfChargeCoupon(request);
        return bl ? Result.ok() : Result.failure();
    }

    /**
     * 使用优惠券
     *
     * @param sceneUseCouponDto 入参
     * @author samphin
     * @since 2021-7-15 09:17:14
     */
    @PutMapping("/used")
    public Result<Boolean> updateCouponUseStatus(@RequestBody SceneUseCouponDto sceneUseCouponDto, HttpServletRequest request) {
        boolean bl = this.sceneCouponService.useCoupon(sceneUseCouponDto, request);
        return bl ? Result.ok() : Result.failure();
    }

    /**
     * 给指定用户发放优惠券
     *
     * @author samphin
     * @since 2021-7-15 09:17:14
     */
    @PostMapping("/grant")
    public Result<Boolean> grantCoupon(@RequestBody @Validated SceneGrantCouponDto couponDto) {
        boolean bl = this.sceneCouponService.grantCoupon(couponDto);
        return bl ? Result.ok() : Result.failure();
    }

    /**
     * 领取优惠券接口
     *
     * @param drawCouponDto 入参
     * @author samphin
     * @since 2021-7-15 09:17:14
     */
    @PostMapping("/draw")
    public Result<Boolean> drawCoupon(@RequestBody SceneDrawCouponDto drawCouponDto, HttpServletRequest request) {
        boolean bl = this.sceneCouponService.drawCoupon(drawCouponDto, request);
        return bl ? Result.ok() : Result.failure();
    }

    /**
     * 查询用户未使用的优惠券列表
     *
     * @param userId 用户ID
     * @author samphin
     * @since 2021-7-15 09:17:14
     */
    @GetMapping("/usable/list")
    public Result<List<SceneCouponVo>> queryMyNoUseCouponList(String userId) {
        List<SceneCouponVo> voList = this.sceneCouponService.queryNoUseCouponList(userId);
        return new Result<>(voList);
    }

    /**
     * 查询用户已过期的优惠券列表
     *
     * @param userId 用户ID
     * @author samphin
     * @since 2021-7-15 09:17:14
     */
    @GetMapping("/expired/list")
    public Result<List<SceneCouponVo>> queryPastDueCouponList(String userId) {
        List<SceneCouponVo> voList = this.sceneCouponService.queryExpiredCouponList(userId);
        return new Result<>(voList);
    }

    /**
     * 查询优惠券已使用记录
     *
     * @param queryDto 查询条件
     * @author samphin
     * @since 2021-7-15 09:17:14
     */
    @GetMapping("/used/records")
    public Result<PageBean<SceneCouponUseRecordVo>> queryCouponList(@ModelAttribute SceneCouponUsedRecordQueryDto queryDto) {
        PageBean<SceneCouponUseRecordVo> pageBean = this.sceneCouponService.queryCouponUseRecordList(queryDto);
        return new Result<>(pageBean);
    }

    /**
     * 查询优惠券领取记录
     *
     * @param queryDto 查询条件
     * @author samphin
     * @since 2021-7-15 09:17:14
     */
    @GetMapping("/draw/records")
    public Result<PageBean<SceneCouponDrawRecordVo>> queryCouponDrawList(@ModelAttribute SceneCouponDrawQueryDto queryDto) {
        PageBean<SceneCouponDrawRecordVo> pageBean = this.sceneCouponService.queryCouponDrawList(queryDto);
        return new Result<>(pageBean);
    }

    /**
     * 查询我已领取的优惠券列表
     *
     * @param queryDto 查询条件
     * @author samphin
     * @since 2021-7-15 09:17:14
     */
    @GetMapping("/my/draw/records")
    public Result<PageBean<SceneCouponDrawRecordVo>> queryMyCouponDrawList(@ModelAttribute SceneCouponDrawQueryDto queryDto, HttpServletRequest request) {
        PageBean<SceneCouponDrawRecordVo> pageBean = this.sceneCouponService.queryMyCouponDrawList(queryDto, request);
        return new Result<>(pageBean);
    }
}
